﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ChessIt
{
    public class ChessBoard
    {
        private String[] columnLabel = { "a", "b", "c", "d", "e", "f", "g", "h" };
        private String[] rowLabel = { "8", "7", "6", "5", "4", "3", "2", "1" };

        private const int EMPTY = 6;
        private int[] START_COLOR = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 1, 1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0 };
        private int[] START_PIECE = { 3, 1, 2, 4, 5, 2, 1, 3, 0, 0, 0,
			0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0,
			0, 3, 1, 2, 4, 5, 2, 1, 3 };
        private string[,] ImageFileName = {
			{ "white_pone.png", "white_knight.png",
					"white_bishop.png", "white_castle.png",
					"white_queen.png", "white_king.png" },
			{ "black_pone.png", "black_knight.png",
					"black_bishop.png", "black_castle.png",
					"black_queen.png", "black_king.png" } };

        private Grid BoardGrid, ControlGrid;

        private Square[,] square = new Square[8, 8];

        public bool First = true;

        public TextBlock tbProfile, tbWhiteMarker, tbBlackMarker, tbWhiteMove, tbBlackMove, tbStatus;

        public ChessBoard(Grid board, Grid control)
        {
            BoardGrid = board;
            ControlGrid = control;
            
            PrepareBoard();
            PrepareControl();
        }

        private void PrepareBoard()
        {
            //BoardGrid.ShowGridLines = true;

            for (int i = 0; i < 9; i++)
            {
                RowDefinition rowDef = new RowDefinition();
                if (i == 0)
                    rowDef.Height = new GridLength(1, GridUnitType.Star);
                else
                    rowDef.Height = new GridLength(2, GridUnitType.Star);
                BoardGrid.RowDefinitions.Add(rowDef);

                ColumnDefinition colDef = new ColumnDefinition();
                if (i == 0)
                    colDef.Width = new GridLength(1, GridUnitType.Star);
                else
                    colDef.Width = new GridLength(2, GridUnitType.Star);
                BoardGrid.ColumnDefinitions.Add(colDef);
            }

            for (int i = 1; i < 9; i++)
            {
                TextBlock tb = new TextBlock();
                tb.HorizontalAlignment = HorizontalAlignment.Center;
                tb.VerticalAlignment = VerticalAlignment.Center;
                tb.FontSize = 30;
                tb.FontWeight = FontWeights.Bold;
                tb.Text = columnLabel[i - 1];
                Grid.SetColumn(tb, i);
                Grid.SetRow(tb, 0);
                BoardGrid.Children.Add(tb);

                tb = new TextBlock();
                tb.HorizontalAlignment = HorizontalAlignment.Center;
                tb.VerticalAlignment = VerticalAlignment.Center;
                tb.FontSize = 30;
                tb.FontWeight = FontWeights.Bold;
                tb.Text = rowLabel[i - 1];
                Grid.SetRow(tb, i);
                Grid.SetColumn(tb, 0);
                BoardGrid.Children.Add(tb);
            }

            for (int i = 0; i < 8; i++)
            {
                // chess board cells
                for (int j = 0; j < 8; j++)
                {
                    square[i, j] = new Square(i, j, this);

                    Grid.SetColumn(square[i, j].CellBackground, j + 1);
                    Grid.SetRow(square[i, j].CellBackground, i + 1);
                    BoardGrid.Children.Add(square[i, j].CellBackground);

                    Grid.SetColumn(square[i, j].CellImage, j + 1);
                    Grid.SetRow(square[i, j].CellImage, i + 1);
                    BoardGrid.Children.Add(square[i, j].CellImage);
                }
            }

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    int c = START_COLOR[j + 8 * i];
                    if (c != EMPTY)
                    {
                        int p = START_PIECE[j + 8 * i];
                        square[i, j].SetIcon(ImageFileName[c,p]);                        
                    }
                    else
                        square[i,j].SetIcon(null);
                }
            }


        }

        private void PrepareControl()
        {
            RowDefinition rowDef = new RowDefinition();            
            rowDef.Height = new GridLength(1, GridUnitType.Auto);            
            ControlGrid.RowDefinitions.Add(rowDef);
            rowDef = new RowDefinition();
            rowDef.Height = new GridLength(1, GridUnitType.Auto);            
            ControlGrid.RowDefinitions.Add(rowDef);
            rowDef = new RowDefinition();
            rowDef.Height = new GridLength(1, GridUnitType.Auto);            
            ControlGrid.RowDefinitions.Add(rowDef);
            rowDef = new RowDefinition();
            rowDef.Height = new GridLength(1, GridUnitType.Auto);
            ControlGrid.RowDefinitions.Add(rowDef);

            rowDef = new RowDefinition();            
            rowDef.Height = new GridLength(1, GridUnitType.Star);
            ControlGrid.RowDefinitions.Add(rowDef);

            ColumnDefinition colDef = new ColumnDefinition();            
            colDef.Width = new GridLength(1, GridUnitType.Star);
            ControlGrid.ColumnDefinitions.Add(colDef);
            colDef = new ColumnDefinition();
            colDef.Width = new GridLength(1, GridUnitType.Star);
            ControlGrid.ColumnDefinitions.Add(colDef);
            
            SolidColorBrush greenBrush = new SolidColorBrush();
            greenBrush.Color = System.Windows.Media.Color.FromArgb(255, 0, 255, 0);
            SolidColorBrush whiteBrush = new SolidColorBrush();
            whiteBrush.Color = System.Windows.Media.Color.FromArgb(255, 255, 255, 255);
            SolidColorBrush blackBrush = new SolidColorBrush();
            blackBrush.Color = System.Windows.Media.Color.FromArgb(255, 0, 0, 0);
            SolidColorBrush grayBrush = new SolidColorBrush();
            grayBrush.Color = System.Windows.Media.Color.FromArgb(255, 105, 105, 105);
            
            //<TextBlock Grid.Row="0" Height="Auto" HorizontalAlignment="Left"  Name="Profile" Text="Skill Level:" VerticalAlignment="Top" FontSize="28" Foreground="Lime" Grid.ColumnSpan="2" />
            tbProfile = new TextBlock();
            ControlGrid.Children.Add(tbProfile);
            Grid.SetRow(tbProfile, 0);
            Grid.SetColumn(tbProfile, 0);
            Grid.SetColumnSpan(tbProfile, 2);
            tbProfile.Text = "Skill Level:";
            tbProfile.HorizontalAlignment = HorizontalAlignment.Left;
            tbProfile.VerticalAlignment = VerticalAlignment.Center;
            tbProfile.FontSize = 28;
            tbProfile.Foreground = greenBrush;

            //<Border Grid.Row="1" Grid.Column="0" Background="WhiteSmoke">
            //    <TextBlock Grid.Row="1" Grid.Column="0" Height="Auto" HorizontalAlignment="Center"  Name="WhiteMarker" Text="White to Move" VerticalAlignment="Top" FontSize="28" Foreground="Black" />
            //</Border>
            Border border = new Border();
            ControlGrid.Children.Add(border);
            Grid.SetRow(border, 1);
            Grid.SetColumn(border, 0);
            border.Background = whiteBrush;
            tbWhiteMarker = new TextBlock();
            border.Child = tbWhiteMarker;
            tbWhiteMarker.Text = "White to Move";
            tbWhiteMarker.HorizontalAlignment = HorizontalAlignment.Center;
            tbWhiteMarker.VerticalAlignment = VerticalAlignment.Center;
            tbWhiteMarker.FontSize = 28;
            tbWhiteMarker.Foreground = blackBrush;
                        
            //<TextBlock Grid.Row="1" Grid.Column="1" Height="Auto" HorizontalAlignment="Center"  Name="BlackMarker" Text="Black to Move" VerticalAlignment="Top" FontSize="28" Foreground="White" />
            border = new Border();
            ControlGrid.Children.Add(border);
            Grid.SetRow(border, 1);
            Grid.SetColumn(border, 1);
            border.Background = grayBrush;
            tbBlackMarker = new TextBlock();
            border.Child = tbBlackMarker;
            //ControlGrid.Children.Add(tbBlackMarker);
            //Grid.SetRow(tbBlackMarker, 1);
            //Grid.SetColumn(tbBlackMarker, 1);            
            tbBlackMarker.Text = "Black to Move";
            tbBlackMarker.HorizontalAlignment = HorizontalAlignment.Center;
            tbBlackMarker.VerticalAlignment = VerticalAlignment.Center;
            tbBlackMarker.FontSize = 28;
            tbBlackMarker.Foreground = whiteBrush;

            //<TextBlock Grid.Row="2" Grid.Column="0" Height="Auto" HorizontalAlignment="Center"  Name="WhiteMove" Text="a1-a1" VerticalAlignment="Top" FontSize="28" Foreground="Lime" />
            tbWhiteMove = new TextBlock();
            ControlGrid.Children.Add(tbWhiteMove);
            Grid.SetRow(tbWhiteMove, 2);
            Grid.SetColumn(tbWhiteMove, 0);
            tbWhiteMove.Text = "a1-a1";
            tbWhiteMove.HorizontalAlignment = HorizontalAlignment.Center;
            tbWhiteMove.VerticalAlignment = VerticalAlignment.Center;
            tbWhiteMove.FontSize = 28;
            tbWhiteMove.Foreground = greenBrush;

            //<TextBlock Grid.Row="2" Grid.Column="1" Height="Auto" HorizontalAlignment="Center"  Name="BlackMove" Text="a1-a1" VerticalAlignment="Top" FontSize="28" Foreground="Lime" />
            tbBlackMove = new TextBlock();
            ControlGrid.Children.Add(tbBlackMove);
            Grid.SetRow(tbBlackMove, 2);
            Grid.SetColumn(tbBlackMove, 1);
            tbBlackMove.Text = "a1-a1";
            tbBlackMove.HorizontalAlignment = HorizontalAlignment.Center;
            tbBlackMove.VerticalAlignment = VerticalAlignment.Center;
            tbBlackMove.FontSize = 28;
            tbBlackMove.Foreground = greenBrush;

            tbStatus = new TextBlock();
            ControlGrid.Children.Add(tbStatus);
            Grid.SetRow(tbStatus, 3);
            Grid.SetColumn(tbStatus, 0);
            Grid.SetColumnSpan(tbStatus, 2);
            tbStatus.Text = "Status:";
            tbStatus.HorizontalAlignment = HorizontalAlignment.Left;
            tbStatus.VerticalAlignment = VerticalAlignment.Center;
            tbStatus.FontSize = 28;
            tbStatus.Foreground = greenBrush;
        }
    }
}
